//
//  UIView+YCTools.m
//  Pods
//
//  Created by Apple on 2022/11/2.
//

#import "UIView+YCTools.h"
#import "UIView+FrameHelp.h"

@implementation UIView (YCTools)

- (CGFloat)cornerRadius {
    return self.layer.cornerRadius;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    if (self.layer.cornerRadius != cornerRadius) {
        self.layer.cornerRadius = cornerRadius;
        self.layer.masksToBounds = YES;
    }
}

- (void)setBorderWithColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth {
    self.layer.borderColor = borderColor.CGColor;
    self.layer.borderWidth = borderWidth;
}

- (void)setShadowWithColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowRadius:(CGFloat)shadowRadius {
    self.layer.shadowColor = shadowColor.CGColor;
    self.layer.shadowOffset = shadowOffset;
    self.layer.shadowOpacity = 1;
    self.layer.shadowRadius = shadowRadius;
}

- (void)setShadowWithColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius {
    self.layer.shadowColor = shadowColor.CGColor;
    self.layer.shadowOffset = shadowOffset;
    self.layer.shadowOpacity = shadowOpacity;
    self.layer.shadowRadius = shadowRadius;
}

- (void)setRadiusShadowPath:(UIColor *)shadowColor shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowSide:(YCRadiusShadowPathType)shadowPathSide shadowPathWidth:(CGFloat)shadowPathWidth radiusLocation:(NSInteger)type {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIRectCorner corner ;
        if (type == 1) {
            corner = UIRectCornerTopLeft|UIRectCornerTopRight;
        }else if (type == 2) {
            corner = UIRectCornerAllCorners;
        }else{
            corner = UIRectCornerBottomLeft|UIRectCornerBottomRight;
        }
        if (@available(iOS 11.0,*)) {
            self.layer.cornerRadius = shadowRadius;
            self.layer.maskedCorners = (CACornerMask)corner;
        }else{
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(shadowRadius, shadowRadius)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
            self.frame = self.bounds;
            maskLayer.path = path.CGPath;
            self.layer.mask = maskLayer;
        }
        self.layer.masksToBounds = NO;
        self.clipsToBounds = NO;
        self.layer.shadowColor = shadowColor.CGColor;
        self.layer.shadowOpacity = shadowOpacity;
        //设置抗锯齿边缘
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.layer.shadowOffset = CGSizeZero;
        CGRect shadowRect;
        CGFloat originX = 0;
        CGFloat originY = 0;
        CGFloat originW = self.bounds.size.width;
        CGFloat originH = self.bounds.size.height;
        switch (shadowPathSide) {
            case YCRadiusShadowPathTypeTop:
                shadowRect  = CGRectMake(originX, originY - shadowPathWidth/2, originW,  shadowPathWidth);
                break;
            case YCRadiusShadowPathTypeBottom:
                shadowRect  = CGRectMake(originX, originH -shadowPathWidth/2, originW, shadowPathWidth);
                break;
            case YCRadiusShadowPathTypeLeft:
                shadowRect  = CGRectMake(originX - shadowPathWidth/2, originY, shadowPathWidth, originH);
                break;
            case YCRadiusShadowPathTypeRight:
                shadowRect  = CGRectMake(originW - shadowPathWidth/2, originY, shadowPathWidth, originH);
                break;
            case YCRadiusShadowPathTypeNoTop:
                shadowRect  = CGRectMake(originX -shadowPathWidth/2, originY +1, originW +shadowPathWidth,originH + shadowPathWidth/2 );
                break;
            case YCRadiusShadowPathTypeAll:
                shadowRect  = CGRectMake(originX - shadowPathWidth/2, originY - shadowPathWidth/2, originW +  shadowPathWidth, originH + shadowPathWidth);
                break;
        }
        UIBezierPath *path =[UIBezierPath bezierPathWithRect:shadowRect];
        self.layer.shadowPath = path.CGPath;
    });
}

- (void)setLineWithFrame:(CGRect)frame color:(UIColor *)color {
    CALayer *CALine = CALayer.new;
    CALine.frame = frame;
    CALine.backgroundColor = color.CGColor;
    [self.layer addSublayer:CALine];
}

- (void)radiusWithRadius:(CGFloat)radius corner:(UIRectCorner)corner {
    if (@available(iOS 11.0, *)) {
        self.layer.cornerRadius = radius;
        self.layer.maskedCorners = (CACornerMask)corner;
        self.layer.masksToBounds = YES;
    } else {
        //这边好像会出问题 corner kCALayerMaxXMinYCorner
        //UIRectCorner corner = UIRectCornerTopRight | UIRectCornerBottomRight; // 右上和右下进行圆角
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = path.CGPath;
        self.layer.mask = maskLayer;
    }
}

- (void)radiusAllCornerWithRadius:(CGFloat)radius {
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
}

- (BOOL)intersectsWithView:(UIView *)view {
    //都先转换为相对于窗口的坐标，然后进行判断是否重合
    CGRect selfRect = [self convertRect:self.bounds toView:nil];
    CGRect viewRect = [view convertRect:view.bounds toView:nil];
    return CGRectIntersectsRect(selfRect, viewRect);
}

- (UIView *)descendantOrSelfWithClass:(Class)cls {
    if ([self isKindOfClass:cls]) {
        return self;
    }
    for (UIView *child in self.subviews) {
        UIView *it = [child descendantOrSelfWithClass:cls];
        if (it)
            return it;
    }
    return nil;
}

- (UIView *)ancestorOrSelfWithClass:(Class)cls {
    if ([self isKindOfClass:cls]) {
        return self;
    } else if (self.superview) {
        return [self.superview ancestorOrSelfWithClass:cls];
    } else {
        return nil;
    }
}

- (void)removeAllSubviews {
    while (self.subviews.count) {
        UIView* child = self.subviews.lastObject;
        [child removeFromSuperview];
    }
}

- (CGRect)convertRectToView:(UIView *)superView {
    CGRect rect = [self convertRect:self.bounds toView:superView];
    return rect;
}

- (CGPoint)offsetFromView:(UIView *)otherView {
    CGFloat x = 0, y = 0;
    for (UIView *view = self; view && view != otherView; view = view.superview) {
        x += view.yc_left;
        y += view.yc_top;
    }
    return CGPointMake(x, y);
}

+ (instancetype)viewFromXib {
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
}

+ (instancetype)viewWithFrame:(CGRect)frame backgroundColor:(nullable UIColor *)backgroundColor {
    UIView *xxView = [[UIView alloc]initWithFrame:frame];
    if (backgroundColor) {
        xxView.backgroundColor = backgroundColor;
    }else{
        xxView.backgroundColor = UIColor.clearColor;
    }
    return xxView;
}

+ (instancetype)viewWithBackgroundColor:(nullable UIColor *)backgroundColor {
    UIView *xxView = [[UIView alloc]init];
    if (backgroundColor) {
        xxView.backgroundColor = backgroundColor;
    }else{
        xxView.backgroundColor = UIColor.clearColor;
    }
    return xxView;
}

- (void)addLineViewFrame:(CGRect)frame {
    [self addLineColorEEEEEE:frame];
}

- (void)addLineColorF3F3F3:(CGRect)frame {
    [self addLineColorEEEEEE:frame];
}

- (void)addLineColorEEEEEE:(CGRect)frame {
    CALayer *CALine = CALayer.new;
    CGRect rect = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 0.5);
    CALine.frame = rect;
    CALine.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1.0].CGColor;
    [self.layer addSublayer:CALine];
}

@end
